# Support for out-of-tree builds (VPATH)
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
VPATH = @srcdir@

CFLAGS=@CFLAGS@
AM_CFLAGS=@NDPI_CFLAGS@

INC=-I $(top_srcdir)/src/include/ -I $(top_builddir)/src/include/ -I $(top_srcdir)/src/lib/third_party/include/
LIB=$(top_builddir)/src/lib/libndpi.a @ADDITIONAL_LIBS@ @LIBS@
GEN_HEADERS=$(top_builddir)/src/include/ndpi_config.h $(top_builddir)/src/include/ndpi_define.h

TOOLS=substringsearch patriciasearch gcrypt-int gcrypt-gnu strnstr
TESTS=substring_test patricia_test strnstr_test geo_test


all: $(TESTS)

tools: $(TOOLS)

gcrypt-int: $(srcdir)/gcrypt.c Makefile $(GEN_HEADERS)
	$(CC) $(INC) $(AM_CFLAGS) $(CFLAGS) $(srcdir)/gcrypt.c -o $@

gcrypt-gnu: $(srcdir)/gcrypt.c Makefile $(GEN_HEADERS)
	$(CC) $(INC) $(AM_CFLAGS) $(CFLAGS) -DHAVE_LIBGCRYPT $(srcdir)/gcrypt.c -o $@ -lgcrypt

substringsearch: $(srcdir)/substringsearch.c Makefile $(GEN_HEADERS)
	$(CC) $(INC) $(AM_CFLAGS) $(CFLAGS) $(srcdir)/substringsearch.c -o substringsearch $(LIB)

strnstr: $(srcdir)/strnstr.cpp Makefile $(GEN_HEADERS)
	$(CXX) $(INC) $(AM_CFLAGS) $(CFLAGS) $(srcdir)/strnstr.cpp -o strnstr

geo: $(srcdir)/geo.c Makefile $(GEN_HEADERS)
	$(CC) $(INC) $(AM_CFLAGS) $(CFLAGS) $(srcdir)/geo.c -o geo $(LIB)

substring_test: substringsearch top-1m.csv
	./substringsearch

strnstr_test: strnstr
	./strnstr

geo_test: geo
	./geo

#

patriciasearch: $(srcdir)/patriciasearch.c Makefile $(GEN_HEADERS)
	$(CC) $(INC) $(AM_CFLAGS) $(CFLAGS) $(srcdir)/patriciasearch.c -o patriciasearch $(LIB)

patricia_test: patriciasearch blacklist-ip.txt
	./patriciasearch


# Alexa Top 1 Million
top-1m.csv:
	wget http://s3.amazonaws.com/alexa-static/top-1m.csv.zip
	unzip top-1m.csv.zip

blacklist-ip.txt:
	wget http://blacklists.ntop.org/blacklist-ip.txt

clean:
	$(RM) $(TOOLS) $(TESTS) *.o *.lo
	$(RM) *zip top-1m.csv blacklist-ip.txt
	$(RM) -r .libs .deps

distclean: clean
	$(RM) Makefile

check:
	true # nothing to do here, tests run via make all

